﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Dixon_Q_Test
{
    class listElement
    {
        private List<Element> l;

        public listElement()
        {
            this.l = new List<Element>();
        }

        public listElement(Element e)
        {
            this.l = new List<Element>();
            this.l.Add(e);
        }

        public listElement(listElement le)
        {
            this.l = new List<Element>();
            int n = le.l.Count();
            for (int i = 0; i < n; i++)
            {
                this.l.Add(le.l[i]);
            }
        }
        public Element getElement(int i)
        {
            return this.l[i]; 
        }


        public bool readDatabase(string file)
        {
            StreamReader sr = new StreamReader(file);
            try
            { 
                int n = int.Parse(sr.ReadLine());
                string str = " , ";
                string[] s = str.Split(',');
                for (int i = 0; i < n; i++)
                {
                    str = sr.ReadLine();
                    s = str.Split(',');
                    Element e = new Element(s[0], float.Parse(s[1]));
                    this.l.Add(e);
                }
                sr.Close();
            }
            catch
            {
                sr.Close();
                return false;
            }
            return true;
        }

        public void writeData(string file)
        {
            StreamWriter sw = new StreamWriter(file);
            int n = this.l.Count();
            sw.WriteLine(n);
            for (int i = 0; i < n; i++)
            {
                sw.WriteLine(this.l[i].getName() + "," + this.l[i].getHeight().ToString());
            }
            sw.Close();
        }

        public void sort()
        {
            int n = this.l.Count;
            for (int i = 0; i < n-1; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (l[i].getHeight() > l[j].getHeight())
                    {
                        Element temp = new Element(l[i]);
                        l[i].equal(l[j]);
                        l[j].equal(temp);
                    }

                }
            }
        }

        public int getCount()
        {
            return this.l.Count();
        }

        public void removeElement(int n)
        {
            this.l.RemoveAt(n);
        }

    }
}
